﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace XFControls.XFPanels.XFPanelItems
{
    public class XFItemSimpleText : IXFItem
    {
        public XFItemState State { get; set; }
        public XFPanelBase Parent { get; set; }
        public string Text { get; private set; }
        public XFItemStyle Style { get; set; }
        public Bitmap Buffer { get; set; }
        public XFItemType ItemType { get; set; }
        private int _height;
        private string _originalText;

        public XFItemSimpleText() { init(); }

        public XFItemSimpleText(string text)
        {
            Text = text;
            _originalText = Text;
            init();
        }

        private void init()
        {
            _height = -1;

            Style = new XFItemStyle()
            {
                SelectedBackgroundAlign = XFItemBackgroundAlign.Center,
                TextFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular)
            };

            ItemType = XFItemType.Display;
        }

        public void SetString(string text)
        {
            Text = text;
            _originalText = Text;
            ResetHeight();
        }

        #region IXFItem Members

        public int GetHeight()
        {
            if (_height > 0)
                return _height;

            using (Bitmap bm = new Bitmap(100, 100))
            {
                //int width = Screen.PrimaryScreen.WorkingArea.Width;
                int width = Screen.PrimaryScreen.WorkingArea.Width;

                if (Parent != null)
                {
                    width -= Parent.AlphaBarWidth;

                    if (Parent.ScrollbarVisible)
                        width -= 5;
                }

                var gr = Graphics.FromImage(bm);
                Text = XFControlUtils.GetSizedString(width - 20, Text, Style.TextFont);
                _height = (int)gr.MeasureString(Text, Style.TextFont).Height + 5;
                gr.Dispose();

                return _height;
            }
        }

        public void ResetHeight()
        {
            _height = -1;
            Text = _originalText;
        }

        public void ItemPaint(System.Drawing.Graphics g, int x, int y)
        {
            int width = Screen.PrimaryScreen.WorkingArea.Width;


            if (Parent != null)
            {
                width -= Parent.AlphaBarWidth;

                if (Parent.ScrollbarVisible)
                    width -= 5;
            }
            XFControlUtils.DrawBackground(Style, g, x, y, width, _height);

            using (Pen p = new Pen(Color.Black))
            {
                g.DrawLine(p, 0, y + (_height - 1), width, y + (_height - 1));
            }
            XFControlUtils.DrawJustifiedString(g, x, y + 2, width, Text, Style.TextFont, Color.Black);
        }

        public XFItemClickResult GetClickResult()
        {
            return XFItemClickResult.Nothing;
        }
        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            if (Buffer != null)
                Buffer.Dispose();

            Buffer = null;

            if (Style != null)
                Style.Dispose();

            Style = null;
        }

        #endregion
    }
}
